package com.yellowpage.bigdata.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.yellowpage.bigdata.data.NspFatallogCommuAssDayRegionSum;
import com.yellowpage.bigdata.service.NspFatallogCommuAssDayRegionSumService;
import com.yellowpage.ca.data.User;
import com.yellowpage.ca.service.RegionService;
import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.struts.BaseAction;

public class NspFatallogCommuAssDayRegionSumAction extends BaseAction {

	public String RESULT_QUERY = "query";
	public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";

	private Configuration configuration;

	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}

	private List<NspFatallogCommuAssDayRegionSum> nspFatallogCommuAssDayRegionSumList = new ArrayList<NspFatallogCommuAssDayRegionSum>();
	private List<Map> resultList = new ArrayList<Map>();

	private NspFatallogCommuAssDayRegionSum nspFatallogCommuAssDayRegionSum;
	private NspFatallogCommuAssDayRegionSumService nspFatallogCommuAssDayRegionSumService;

	private String selectedId;
	private String[] selectedIds;
	private String message;

	private String query_note;
	private String query_logDate;
	private String query_regionCode;
	private String query_createTime;
	private String query_fatallogNumDayregionSumId;
	private String query_numnDayCount;
	private String query_numDayCount;
	private String query_numnAllCount;
	private String query_numAllCount;
	private Map<String, String> regionMap;
	private RegionService regionService;
	private String regionName;
	private String query_logDate_end;
	public String getQuery_logDate_end() {
		return query_logDate_end;
	}

	public void setQuery_logDate_end(String query_logDate_end) {
		this.query_logDate_end = query_logDate_end;
	}

	public String getRegionName() {
		return regionName;
	}

	public void setRegionName(String regionName) {
		this.regionName = regionName;
	}

	public Map<String, String> getRegionMap() {
		return regionMap;
	}

	public void setRegionMap(Map<String, String> regionMap) {
		this.regionMap = regionMap;
	}

	public RegionService getRegionService() {
		return regionService;
	}

	public void setRegionService(RegionService regionService) {
		this.regionService = regionService;
	}

	public String getRegionSql() {
		Map session = ActionContext.getContext().getSession();
		User user = (User) session.get("userObject");
		String function_code = "OMC_ABILITYCALL_LOG_COUNT_DAY_CHECKCOUNT";
		String regionsql = regionService.getRegionclauseByUserFunction(
				user.getUserId(), function_code, "REGION_CODE");
		return regionsql;
	}

	public void prepare() {
		super.prepare();

		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.nspFatallogCommuAssDayRegionSum = nspFatallogCommuAssDayRegionSumService
					.getNspFatallogCommuAssDayRegionSum(Long
							.parseLong(selectedId));
		}
	}

	public String queryByPage() {
		initPageRender();

		Map queryMap = new HashMap();
		// 地区
		String str = "(";
		if (!regionName.equals("")) {
			regionMap = nspFatallogCommuAssDayRegionSumService
					.getRegionMap(getRegionSql());
			String[] val = regionName.split(";");
			str = "(";
			for (String ss : val) {
				for (String key : regionMap.keySet()) {
					if (regionMap.get(key).contains(ss)) {
						str += "'" + key + "',";
						break;
					}
				}
			}
			str = str.substring(0, str.length() - 1) + ")";
			queryMap.put("regionCode", str);
		} else {
			queryMap.put("regionCode", regionName);
		}
		queryMap.put("logDate", query_logDate);
		queryMap.put("logDate_end", query_logDate_end);
		resultList = nspFatallogCommuAssDayRegionSumService
				.queryNspFatallogCommuAssDayRegionSumByPage(queryMap,
						pageRender);

		setPageRenderToRequest();

		return RESULT_QUERY;
	}

	public String queryChannel() {
		initPageRender();
		regionMap = nspFatallogCommuAssDayRegionSumService
				.getRegionMap(getRegionSql());
		setPageRenderToRequest();
		return "toRegion";
	}

	public String toAdd() {
		return RESULT_ADD;
	}

	public String save() {

		nspFatallogCommuAssDayRegionSumService
				.saveNspFatallogCommuAssDayRegionSum(nspFatallogCommuAssDayRegionSum);

		message = "新增成功!";
		return RESULT_ADD;
	}

	public void validateSave() {
		if (nspFatallogCommuAssDayRegionSum.getNote().length() == 0) {
			this.addActionError("** Note 不能为空 !");
		}
		if (nspFatallogCommuAssDayRegionSum.getLogDate().length() == 0) {
			this.addActionError("** LogDate 不能为空 !");
		}
		if (nspFatallogCommuAssDayRegionSum.getRegionCode().length() == 0) {
			this.addActionError("** RegionCode 不能为空 !");
		}

		this.setValidatorResult("/bigdata/NspFatallogCommuAssDayRegionSumAdd.jsp");
	}

	public String toEdit() {
		Long id = new Long(selectedId);

		nspFatallogCommuAssDayRegionSum = nspFatallogCommuAssDayRegionSumService
				.getNspFatallogCommuAssDayRegionSum(id);

		return RESULT_EDIT;
	}

	public String update() {

		nspFatallogCommuAssDayRegionSumService
				.updateNspFatallogCommuAssDayRegionSum(nspFatallogCommuAssDayRegionSum);

		message = "修改成功!";

		return RESULT_EDIT;
	}

	public void validateUpdate() {
		if (nspFatallogCommuAssDayRegionSum.getNote().length() == 0) {
			this.addActionError("** Note 不能为空 !");
		}
		if (nspFatallogCommuAssDayRegionSum.getLogDate().length() == 0) {
			this.addActionError("** LogDate 不能为空 !");
		}
		if (nspFatallogCommuAssDayRegionSum.getRegionCode().length() == 0) {
			this.addActionError("** RegionCode 不能为空 !");
		}
		this.setValidatorResult("/bigdata/NspFatallogCommuAssDayRegionSumUpdate.jsp");
	}

	public String delete() {
		List ids = new ArrayList();

		for (String id : selectedIds) {
			ids.add(Long.valueOf(id));
		}

		try {
			this.nspFatallogCommuAssDayRegionSumService.delete(ids);
		} catch (Exception e) {
			System.out.println("异常....");
			this.addActionError("** 可能含有子记录约束冲�?删除失败 !");
		}

		return queryByPage();
	}

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	public void setNspFatallogCommuAssDayRegionSum(
			NspFatallogCommuAssDayRegionSum nspFatallogCommuAssDayRegionSum) {
		this.nspFatallogCommuAssDayRegionSum = nspFatallogCommuAssDayRegionSum;
	}

	public NspFatallogCommuAssDayRegionSum getNspFatallogCommuAssDayRegionSum() {
		return nspFatallogCommuAssDayRegionSum;
	}

	public void setNspFatallogCommuAssDayRegionSumService(
			NspFatallogCommuAssDayRegionSumService nspFatallogCommuAssDayRegionSumService) {
		this.nspFatallogCommuAssDayRegionSumService = nspFatallogCommuAssDayRegionSumService;
	}

	public List<NspFatallogCommuAssDayRegionSum> getNspFatallogCommuAssDayRegionSumList() {
		return nspFatallogCommuAssDayRegionSumList;
	}

	public void setNspFatallogCommuAssDayRegionSumList(
			List<NspFatallogCommuAssDayRegionSum> nspFatallogCommuAssDayRegionSumList) {
		this.nspFatallogCommuAssDayRegionSumList = nspFatallogCommuAssDayRegionSumList;
	}

	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}

	public String getQuery_note() {
		return this.query_note;
	}

	public void setQuery_note(String note) {
		this.query_note = note;
	}

	public String getQuery_logDate() {
		return this.query_logDate;
	}

	public void setQuery_logDate(String logDate) {
		this.query_logDate = logDate;
	}

	public String getQuery_regionCode() {
		return this.query_regionCode;
	}

	public void setQuery_regionCode(String regionCode) {
		this.query_regionCode = regionCode;
	}

	public String getQuery_createTime() {
		return this.query_createTime;
	}

	public void setQuery_createTime(String createTime) {
		this.query_createTime = createTime;
	}

	public String getQuery_fatallogNumDayregionSumId() {
		return this.query_fatallogNumDayregionSumId;
	}

	public void setQuery_fatallogNumDayregionSumId(
			String fatallogNumDayregionSumId) {
		this.query_fatallogNumDayregionSumId = fatallogNumDayregionSumId;
	}

	public String getQuery_numnDayCount() {
		return this.query_numnDayCount;
	}

	public void setQuery_numnDayCount(String numnDayCount) {
		this.query_numnDayCount = numnDayCount;
	}

	public String getQuery_numDayCount() {
		return this.query_numDayCount;
	}

	public void setQuery_numDayCount(String numDayCount) {
		this.query_numDayCount = numDayCount;
	}

	public String getQuery_numnAllCount() {
		return this.query_numnAllCount;
	}

	public void setQuery_numnAllCount(String numnAllCount) {
		this.query_numnAllCount = numnAllCount;
	}

	public String getQuery_numAllCount() {
		return this.query_numAllCount;
	}

	public void setQuery_numAllCount(String numAllCount) {
		this.query_numAllCount = numAllCount;
	}

}
